﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SilverlightApplication1.GdiService;

namespace SilverlightApplication1
{
    public class SearchViewModel : BaseViewModel
    {
        GdiStoryServiceClient _client = new GdiStoryServiceClient();
        public SearchViewModel()
        {
            _client.GetStoriesCompleted += new EventHandler<GetStoriesCompletedEventArgs>(_client_GetStoriesCompleted);
            //_client.GetStoriesAsync();

            _client.SearchGoogleCompleted += new EventHandler<SearchGoogleCompletedEventArgs>(_client_SearchGoogleCompleted);
            //_client.SearchGoogleAsync("Silverlight");

            SearchCommand = new MyCommand(Search, null);

        }

        private void Search(object o)
        {
            _client.SearchGoogleAsync(SearchTerm);
        }

        public ICommand SearchCommand { get; set; }

        void _client_SearchGoogleCompleted(object sender, SearchGoogleCompletedEventArgs e)
        {
            SearchResults = e.Result;
        }


        void _client_GetStoriesCompleted(object sender, GetStoriesCompletedEventArgs e)
        {
            Stories = e.Result;
        }

        private string _searchTerm;
        public string SearchTerm
        {
            get
            {
                return _searchTerm;
            }
            set
            {
                _searchTerm = value;
                RaisePropertyChanged("SearchTerm");
            }
        }
        private ObservableCollection<Story> _stories = new ObservableCollection<Story>();
        public ObservableCollection<Story> Stories
        {
            get
            {
                return _stories;
            }
            set
            {
                _stories = value;
                RaisePropertyChanged("Stories");
            }
        }

        private ObservableCollection<GoogleSearchResult> _searchResults = new ObservableCollection<GoogleSearchResult>();
        public ObservableCollection<GoogleSearchResult> SearchResults
        {
            get
            {
                return _searchResults;
            }
            set
            {
                _searchResults = value;
                RaisePropertyChanged("SearchResults");
            }
        }
    }
}
